package cn.sdormitory.basedata.service.impl;

import cn.sdormitory.basedata.dao.BSubjectDao;
import cn.sdormitory.basedata.entity.BStudent;
import cn.sdormitory.basedata.entity.BSubject;
import cn.sdormitory.basedata.service.BSubjectService;
import cn.sdormitory.common.utils.StringUtils;
import cn.sdormitory.common.utils.SysUserUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @className: BSubjectServiceImpl
 * @author: Alex
 * @date: 2024/12/9
 **/
@Service
public class BSubjectServiceImpl extends ServiceImpl<BSubjectDao, BSubject> implements BSubjectService {
	
	@Override
	public IPage<BSubject> getPage(Map<String, Object> params) {
		int pageSize = Integer.parseInt(String.valueOf(params.get("pageSize")));
		int pageNum = Integer.parseInt(String.valueOf(params.get("pageNum")));
		String subjectName = (String) params.get("subjectName");
		String status = (String) params.get("status");
		
		LambdaQueryWrapper<BSubject> wrapper = new LambdaQueryWrapper<>();
		wrapper.eq(StringUtils.isNotEmpty(subjectName), BSubject::getSubjectName, subjectName);
		wrapper.eq(StringUtils.isNotEmpty(status), BSubject::getStatus, status);
		return page(new Page<>(pageNum, pageSize), wrapper);
	}
	
	@Override
	public List<BSubject> getSubjectList() {
		LambdaQueryWrapper<BSubject> wrapper = new LambdaQueryWrapper<>();
		wrapper.eq( BSubject::getStatus, "1");
		return this.baseMapper.selectList(wrapper);
	}
	
	@Override
	public int createOrUpdateSubject(BSubject bSubject) {
		if (Objects.isNull(bSubject.getId())) {
			// 新增
			bSubject.setStatus("1");
			bSubject.setCreateTime(new Date());
			bSubject.setCreateBy(SysUserUtils.getSysUser().getUsername());
			return this.baseMapper.insert(bSubject);
		}
		// 修改
		bSubject.setModifyBy(SysUserUtils.getSysUser().getUsername());
		bSubject.setModifyTime(new Date());
		return this.baseMapper.updateById(bSubject);
	}
	
	@Override
	public int deleteByIds(Long id) {
		return this.baseMapper.deleteById(id);
	}
	
	@Override
	public int updateStatus(Long id, String status) {
		return this.baseMapper.update(null, new UpdateWrapper<BSubject>().lambda()
				.set(BSubject::getStatus, status)
				.set(BSubject::getModifyBy, SysUserUtils.getSysUser().getUsername())
				.set(BSubject::getModifyTime, new Date())
				.eq(BSubject::getId,id));
	}
}
